<template>
  <div class="app-container">
    <el-form ref="form" label-width="120px">
      <el-form-item v-loading="listLoading" label="选择上交作业">
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in list"
            :key="item.oid"
            :value="item.oid"
            :label="item.title"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上传作业">
        <el-upload
          ref="upload"
          class="upload-demo"
          drag
          multiple
          :action="uploadUrl"
          :data="formdata"
          :on-success="handleSuccess"
          :on-error="handleError"
          :on-preview="handlePreview"
          :on-change="beforeUpload"
          :file-list="form.fileList"
          :limit="1"
          :on-exceed="handleExceed"
          :before-remove="beforeRemove"
          :auto-upload="false"
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div slot="tip" class="el-upload__tip">文件大小请不要超过5mb</div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button style="margin-left: 10px;" type="success" @click="submitUpload">上传</el-button>
        <el-button @click="onCancel">Cancel</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable */
import { getToken } from '../../utils/auth'
import { parseTime } from '../../utils'
import { fetchHomework } from '@/api/homework'
export default {
  
  data() {
    return {
      uploadUrl : process.env.VUE_APP_BASE_API + "/file/upload",
      form: {
        type: '',
        date: '',
        fileList: [],
      },
      value: '',
      formdata: null,
      list: [],
      listLoading: false,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      fetchHomework(this.listQuery).then(response => {
        this.list = response.items
        this.total = response.total
        this.listLoading = false
      })
    },
    // 该方法已经不会被调用
    handleUpload(f){
      axios({
        url: f.action,
        method: 'post',
        data: f.file,
        params: {
          token: getToken(),
          homeworkId: this.value,
          date: parseTime(new Date())
        },
      })
      .then((response) =>{
        console.log("submit status: " + response.status)
        if(response.status == 200){
          this.$message({
            message: 'success',
            type: 'success'
          })
        }else{
          this.$message({
            message: 'failed!',
            type: 'error'
          })
        }
      },(err) =>{
        console.log(err)
        this.$message({
          message: 'failed!',
          type: 'error'
        })
      })
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    beforeUpload() {
      // 包装上传携带的数据
      this.formdata = {
        token: getToken(),
        homeworkId: this.value,
        date: parseTime(new Date()),
        user: this.$store.getters.name
      }
      console.log(this.formdata)
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleSuccess (response, file, fileList) {
      this.$message({
        message: 'success',
        type: 'success'
      })
    },
    handleError (err) {
      console.log(err)
      this.$message({
        message: 'failed!',
        type: 'error'
      })
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning('一次最多上传一个文件!')
    },
    beforeRemove(file, fileList) {
      return this.$confirm('确认移除 ' + file.name + ' ?')
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

